<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/15
 * Time: 16:21
 */


namespace App\Transformers;

use App\Models\UserBase;
use League\Fractal\TransformerAbstract;
use Illuminate\Support\Str;

class UserTransformer extends TransformerAbstract
{

    /**
     * @param UserBase $user
     * @return array
     */
    public function transform(UserBase $user)
    {
        //  返回用户好友信息

        return [
            'user_id'       => $user->id,
            'account'       => $user->account,
            'user_name'     => $user->name,
            'hospital_id'     => $user->hospital_id,
            'created_at'     => $user->created_at->toDateString(),
            'pic'    => $this->getPic($user),

        ];
    }

    public function getPic(UserBase $user){


        if($user->type == 1){
            $pic       =  'http://www.365tang.cn/upload/face/user_man.png';
            if($user->patient){
                if($user->patient->PIC){
                    if (!Str::startsWith($user->patient->PIC, 'http')) {
                        $pic  = 'http://www.365tang.cn/upload/face/' . $user->patient->PIC;
                    }else{
                        $pic       =  $user->patient->PIC;
                    }
                }
            }

        }else{

            if($user->sex == 1){
                $pic       =  'http://www.365tang.cn/upload/public/doctor_female.png';
            }else{
                $pic       =  'http://www.365tang.cn/upload/public/doctor_male.png';
            }

            if($user->detail){
                if($user->detail->PIC){
                    if (!Str::startsWith($user->detail->PIC, 'http')) {
                        $pic   =   'http://www.365tang.cn/upload/'.$user->detail->PIC;
                    }else{
                        $pic       =  $user->detail->PIC;
                    }

                }
            }

        }
        return $pic;
    }


}
